package com.hhkj.generator.common;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.hhkj.generator.entity.TableItem;
import com.hhkj.generator.util.PathUtil;
import org.apache.commons.lang.StringUtils;

import java.util.*;

public class CreateCodeUtil {

    private static final String VAR_PATH_BASE = "src/main/java/com/hhkj/${projectCode}/admin/${packageCode}";

    private static final String VAR_PATH_BASE_XML = "src/main/resources/mapper/${packageCode}";

    private static final String PATH_ENTITY = "/entity";
    private static final String PATH_ENTITY_PODO = "/model/podo";
    private static final String PATH_ENTITY_REQ = "/model/req";
    private static final String PATH_ENTITY_RES = "/model/res";
    private static final String PATH_SERVICE_BIZ = "/service/biz";
    private static final String PATH_SERVICE_BIZ_IMPL = "/service/biz/impl";
    private static final String PATH_SERVICE_EXTEND = "/service/extend";
    private static final String PATH_SERVICE_EXTEND_IMPL = "/service/extend/impl";
    private static final String PATH_SERVICE_BASE = "/service/base";
    private static final String PATH_SERVICE_BASE_IMPL = "/service/base/impl";
    private static final String PATH_CONTROLLER = "/controller";
    private static final String PATH_MAPPER = "/mapper";

    public static void createCode(TableItem detail, String projectCode, String targetPath, String sqlPath) throws Exception {

        /* ============================================================================================= */
        String objectCode = detail.getObjectCode();                //类名				========2
        String tablePrefix = detail.getTablePrefix();                    //表后缀				========3
        String tableName = detail.getTableName();                    //表名				========3
        String tableCode = detail.getTableCode();                    //表名				========3
        tablePrefix = null == tablePrefix ? "" : tablePrefix.toLowerCase();        //表后缀转小写
        tableName = StringUtils.isEmpty(tableName) ? objectCode.toLowerCase() : tableName;        //类名转大写
		String packageCode = detail.getPackageCode();

        Map<String, Object> paramMap = new HashMap<String, Object>();        //创建数据模型
        paramMap.put("fieldList", detail.getFieldList());
        paramMap.put("packageCode", packageCode);                        //包名
        paramMap.put("objectCode", objectCode);                            //类名
        paramMap.put("objectCodeLower", objectCode.toLowerCase());        //类名(全小写)
        paramMap.put("objectCodeUpper", objectCode.toUpperCase());        //类名(全大写)
        paramMap.put("tableName", tableName);
        paramMap.put("tableCode", tableCode);
        paramMap.put("tablePrefix", tablePrefix);                        //表前缀
        paramMap.put("nowDate", new Date());                            //当前日期
        paramMap.put("objectCodeFirstLower", objectCode.substring(0, 1).toLowerCase() + objectCode.substring(1));
        // 去除前缀后表内容按照_切分成
        List<String> tableContentList = StrUtil.split(detail.getTableRealName(), "_");
        paramMap.put("objectCodeSuffixName", getControllerSuffixName(tableContentList, packageCode));
        paramMap.put("projectCode", projectCode);
        paramMap.put("tableCodeAbbr", getTableAbbr(tableCode));

        /* ============================================================================================= */
        String ftlPath = PathUtil.getClassResources() + "/ftl";                //模板路径
        String filePath = targetPath;                        //存放路径

        String basePath = VAR_PATH_BASE.replace("${projectCode}", projectCode).replace("${packageCode}", packageCode);
        String baseXmlPath = VAR_PATH_BASE_XML.replace("${projectCode}", projectCode).replace("${packageCode}", packageCode);

        String controllerPath = basePath + PATH_CONTROLLER;
        String serviceBasePath = basePath + PATH_SERVICE_BASE;
        String serviceBaseImplPath = basePath + PATH_SERVICE_BASE_IMPL;
        String serviceBizPath = basePath + PATH_SERVICE_BIZ;
        String serviceBizImplPath = basePath + PATH_SERVICE_BIZ_IMPL;
        String serviceExtendPath = basePath + PATH_SERVICE_EXTEND;
        String serviceExtendImplPath = basePath + PATH_SERVICE_EXTEND_IMPL;
        String entityPath = basePath + PATH_ENTITY;
        String entityDoPath = basePath + PATH_ENTITY_PODO;
        String entityPoPath = basePath + PATH_ENTITY_PODO;
        String entityReqPath = basePath + PATH_ENTITY_REQ;
        String entityResPath = basePath + PATH_ENTITY_RES;
        String mapperPath = basePath + PATH_MAPPER;
        String mapperXmlPath = baseXmlPath;

        /*生成controller*/
        FreemarkerWriter.write("controllerTemplate.ftl", paramMap, controllerPath + "/" + objectCode + "Controller.java", filePath, ftlPath);

        /*生成entity*/
        FreemarkerWriter.write("entityTemplate.ftl", paramMap, entityPath + "/" + objectCode + ".java", filePath, ftlPath);
        FreemarkerWriter.write("entityDoTemplate.ftl", paramMap, entityDoPath + "/" + objectCode + "Do.java", filePath, ftlPath);
        FreemarkerWriter.write("entityPoTemplate.ftl", paramMap, entityPoPath + "/" + objectCode + "Po.java", filePath, ftlPath);
        FreemarkerWriter.write("entityReqTemplate.ftl", paramMap, entityReqPath + "/" + objectCode + "Req.java", filePath, ftlPath);
        FreemarkerWriter.write("entityResTemplate.ftl", paramMap, entityResPath + "/" + objectCode + "Res.java", filePath, ftlPath);

        /*生成service*/
        FreemarkerWriter.write("serviceTemplate.ftl", paramMap, serviceBasePath + "/" + objectCode + "Service.java", filePath, ftlPath);
        FreemarkerWriter.write("serviceImplTemplate.ftl", paramMap, serviceBaseImplPath + "/" + objectCode + "ServiceImpl.java", filePath, ftlPath);
        FreemarkerWriter.write("bizServiceTemplate.ftl", paramMap, serviceBizPath + "/" + objectCode + "BizService.java", filePath, ftlPath);
        FreemarkerWriter.write("bizServiceImplTemplate.ftl", paramMap, serviceBizImplPath + "/" + objectCode + "BizServiceImpl.java", filePath, ftlPath);
        FreemarkerWriter.write("extendServiceTemplate.ftl", paramMap, serviceExtendPath + "/" + objectCode + "ExtendService.java", filePath, ftlPath);
        FreemarkerWriter.write("extendServiceImplTemplate.ftl", paramMap, serviceExtendImplPath + "/" + objectCode + "ExtendServiceImpl.java", filePath, ftlPath);

        /*生成mybatis xml*/
        FreemarkerWriter.write("mapperTemplate.ftl", paramMap, mapperPath + "/" + objectCode + "Mapper.java", filePath, ftlPath);
        FreemarkerWriter.write("mapperXmlTemplate.ftl", paramMap, mapperXmlPath + "/" + objectCode + "Mapper.xml", filePath, ftlPath);

        /*生成SQL脚本*/
         FreemarkerWriter.write("mysqlSQLTemplate.ftl", paramMap, tablePrefix + tableName + ".sql", sqlPath, ftlPath);
         FreemarkerWriter.write("apiJsonTemplate.ftl", paramMap, tablePrefix + tableName + ".txt", sqlPath, ftlPath);
    }

    private static String getControllerSuffixName(List<String> tableSplitList, String prefixObjectCodeName){
        tableSplitList.remove(prefixObjectCodeName);
        String separator = "";
        if (CollectionUtil.isNotEmpty(tableSplitList)) {
            separator = StrUtil.SLASH;
        }
        return separator + StrUtil.join(StrUtil.SLASH, tableSplitList);
    }

    private static String getTableAbbr(String tableCode){
        String[] array = tableCode.split("_");
        String rs = "";
        for (String str : array) {
            rs += str.charAt(0);
        }
        return rs;
    }

    public static void main(String[] args) {
        String rs = getTableAbbr("x_prj_budget_detail_category");
        System.out.println(rs);
    }

}
